//
//  DataViewController.m
//  XiXi
//
//  Created by HawkingWang on 16/9/29.
//  Copyright © 2016年 xi-xi.cc. All rights reserved.
//

#import "DataViewController.h"

#import "DataTableViewCell.h"//cell

@interface DataViewController () <UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *alreadyDataTableView;
@property (nonatomic, strong) UITableView *prepareDataTableView;


@end

@implementation DataViewController
{
    UIScrollView *_scrollView;
    
    UIView *_slider;
    
    CGFloat _tempX;
}

 static NSString *const DataTableViewCellID = @"DataTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tempX = (SCREEN_WIDTH / 2 - 90) / 2;
    self.view.backgroundColor = RGBCOLOR(236, 234, 228);
    
    [self createSlider];
    
    [self setScrollView];
    
    //注册Tableview
    [self registerTableView];
}

- (void)createSlider{
    UIButton *alreadyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    alreadyBtn.frame = CGRectMake(0, 4, SCREEN_WIDTH / 2, 50);
    alreadyBtn.backgroundColor = [UIColor whiteColor];
    [alreadyBtn setTitle:@"已准备数据" forState:UIControlStateNormal];
    alreadyBtn.titleLabel.font = [UIFont fontWithName:@"PingFang SC" size:15];
    [alreadyBtn setTitleColor:RGBCOLOR(34, 36, 45) forState:UIControlStateNormal];
    [alreadyBtn addTarget:self action:@selector(alreadyClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:alreadyBtn];
    
    UIButton *prepareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    prepareBtn.frame = CGRectMake(SCREEN_WIDTH / 2, 4, SCREEN_WIDTH / 2, 50);
    prepareBtn.backgroundColor = [UIColor whiteColor];
    [prepareBtn setTitle:@"待准备数据" forState:UIControlStateNormal];
    prepareBtn.titleLabel.font = [UIFont fontWithName:@"PingFang SC" size:15];
    [prepareBtn setTitleColor:RGBCOLOR(34, 36, 45) forState:UIControlStateNormal];
    [prepareBtn addTarget:self action:@selector(prepareClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:prepareBtn];
    
    _slider = [[UIView alloc] initWithFrame:CGRectMake(_tempX, 41.5, 95, 1)];
    _slider.backgroundColor = RGBCOLOR(187, 188, 190);
    [self.view addSubview:_slider];
}

#pragma mark === 设置ScrollView ===
- (void)setScrollView{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,  54, SCREEN_WIDTH, SCREEN_HEIGHT)];
    //实际尺寸
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, SCREEN_HEIGHT - 49 - 50);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;//按页滚动
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
}

- (void)registerTableView{
    [self.alreadyDataTableView registerNib:[UINib nibWithNibName:@"DataTableViewCell" bundle:nil] forCellReuseIdentifier:DataTableViewCellID];
    [self.prepareDataTableView registerNib:[UINib nibWithNibName:@"DataTableViewCell" bundle:nil] forCellReuseIdentifier:DataTableViewCellID];
}
#pragma mark === ScrollViewDelegate ===
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        
        if (scrollView.contentOffset.x < 0) {
            _slider.frame = CGRectMake(_tempX, 41.5, 95, 1);
        }else if (scrollView.contentOffset.x > SCREEN_WIDTH){
            _slider.frame = CGRectMake(SCREEN_WIDTH / 2 + _tempX, 41.5, 95, 1);
        }else{
            CGFloat X =  _tempX + scrollView.contentOffset.x / 2;
            _slider.frame = CGRectMake(X, 41.5, 95, 1);
        }
    }
}
//点击已准备数据
- (void)alreadyClick:(id)sender {
    [UIView animateWithDuration:0.1 animations:^{
        _slider.frame = CGRectMake(_tempX, 41.5, 95, 1);
    } completion:^(BOOL finished) {
        _scrollView.contentOffset = CGPointMake(0, 0);
    }];
}
//点击待准备数据
- (void)prepareClick:(id)sender {
    [UIView animateWithDuration:0.1 animations:^{
        _slider.frame = CGRectMake(SCREEN_WIDTH / 2 + _tempX, 41.5, 95, 1);
    } completion:^(BOOL finished) {
        _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }];
}

#pragma mark === TableViewDelegate ===
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 145;
}
//Reusable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DataTableViewCellID];
    //已准备数据
    if (tableView == _alreadyDataTableView) {
//        [cell loadData:<#(DataModel *)#>];
        cell.title.text = @"阿加莎推理巨作——爱乐汇·拉得斯基进行曲——世界";
        cell.time.text = @"2016.01.30 19:30 ~ 21:30";
        cell.theatre.text = @"西溪天堂艺术中心剧院名称";
        cell.hall.text = @"Interting演出厅名称";
        
        if (indexPath.row == 1) {
            //清除数据缓存 按钮设置
            cell.dataBtn.layer.borderWidth = 0;
            [cell.dataBtn setTitle:@"清除数据缓存" forState:UIControlStateNormal];
            [cell.dataBtn setTitleColor:RGBCOLOR(149, 145, 167) forState:UIControlStateNormal];
            cell.dataBtn.userInteractionEnabled = NO;
        }
        cell.dataBtnClick = ^(){
            DLog(@"提交检票信息");
        };
    }
    //待准备数据
    else{
//        [cell loadData:<#(DataModel *)#>];
        cell.title.text = @"阿加莎推理巨作——爱乐汇·拉得斯基进行曲——世界";
        cell.time.text = @"2016.01.30 19:30 ~ 21:30";
        cell.theatre.text = @"西溪天堂艺术中心剧院名称";
        cell.hall.text = @"Interting演出厅名称";
        
        [cell.dataBtn setTitle:@"准备检票数据" forState:UIControlStateNormal];
        
        if (indexPath.row == 1) {
            //数据下载中 按钮设置
            [cell.dataBtn setTitle:@"数据下载中..." forState:UIControlStateNormal];
            [cell.dataBtn setTitleColor:RGBCOLOR(149, 145, 167) forState:UIControlStateNormal];
            cell.dataBtn.layer.borderColor = RGBCOLOR(149, 145, 167).CGColor;
            cell.dataBtn.userInteractionEnabled = NO;
        }
        
        cell.dataBtnClick = ^(){
            DLog(@"准备检票信息");
        };
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableView *)alreadyDataTableView{
    if (!_alreadyDataTableView) {
        _alreadyDataTableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 50 - 68)style:UITableViewStylePlain];
        _alreadyDataTableView.delegate = self;
        _alreadyDataTableView.dataSource = self;
        _alreadyDataTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _alreadyDataTableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        _alreadyDataTableView.backgroundColor = RGBCOLOR(236, 234, 228);
        [_scrollView addSubview:_alreadyDataTableView];
    }
    return _alreadyDataTableView;
}
- (UITableView *)prepareDataTableView{
    if (!_prepareDataTableView) {
        _prepareDataTableView= [[UITableView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH,0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 50 - 68)style:UITableViewStylePlain];
        _prepareDataTableView.delegate = self;
        _prepareDataTableView.dataSource = self;
        _prepareDataTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _prepareDataTableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        _prepareDataTableView.backgroundColor = RGBCOLOR(236, 234, 228);
        [_scrollView addSubview:_prepareDataTableView];
    }
    return _prepareDataTableView;
}

@end
